package com.qing.study.serviceImpl;

import com.qing.study.comm.Result;
import com.qing.study.service.MkService;
import com.qing.study.util.OkHttpClientIgnoreSSL;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Service;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

@Service
@Slf4j
public class MkServiceImpl implements MkService {

    @Override
    public Result callMk() throws NoSuchAlgorithmException {
        MediaType JSON = MediaType.get("application/json; charset=utf-8");
        String token = "AAECAzY2QzY5NEQyNjZDNzNEOTJsaWppbnNodWFuZ9u1lbH6Gs1kh8GQ+AVZ9ywDKlZ8";
        String json = "{\"requestQuery\": {\"token\": \"" + token + "\"}}";
        OkHttpClient client = OkHttpClientIgnoreSSL.getUnsafeOkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url("https://mk.avicsupply.com:4443/openapi/tic-definition/integrationInterface/call?functionCode=function_9hdtf6q45050sh1d&access_token=b06d0b7ca6e2147196e8c28d4a86c900f46282af3535c41343c470e5b4b96980")
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            if (response.body() != null) {
                log.info("请求响应：{}", response.body().string());
            }
            return Result.ok("调用MK接口成功！");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(500, "调用MK接口失败！" + e.getMessage());
        }
    }
}
